A Python Tuple is an immutable
sequence of fixed sized. They are created using round brackets ()
with commas to separate the elements.
In [1]:
('x', 'y', 'z')
Out[1]:
The elements of a tuple need not have the same type.
In [2]:
(1, 'b', 2.5)
Out[2]:
In [3]:
# Assigning a tuple to the variable tup
tup = ('first', 'second', 'third')
In [4]:
tup[0] # Extract the first element.
Out[4]:
In [5]:
tup[1] # Extract the second element.
Out[5]:
In [6]:
tup[2] # Extract the third element.
Out[6]:
In [7]:
tup[3] # Extracting a non-existent element.
Note that this last example results in an error from attempting to extract an element that doesn't exist. It is also possible to extract the elements of a tuple as follows.
In [8]:
a, b, c = tup
print(a)
print(b)
print(c)
The immutable aspect of tuples will be explained in a bit.
A Python List is a mutable sequence. Unlike tuples they don't have a fixed size. They are created using square brackets []
with commas to separate the elements.
In [9]:
["a", "b", "c"]
Out[9]:
Lists can be added together to create larger lists.
In [10]:
[1, 2] + [3, 4] + [5, 6]
Out[10]:
In [11]:
# Creating a list and assigning it to the variable x.
lis = [1, 2, 3, 4, 5]
lis
Out[11]:
In [12]:
lis[0] # Extract the first element.
Out[12]:
In [13]:
lis[1] # Extract the second element.
Out[13]:
In [14]:
lis[-1] # Extract the last element.
Out[14]:
In [15]:
lis[-2] # Extract the second to last element.
Out[15]:
In [16]:
lis[:3] # Extract the first three elements or equivalently
# extract elements up to (but not including) the fourth element.
Out[16]:
In [17]:
lis[3:] # Drop the first three elements and return the rest or equivalently
# extract elements from the fourth element onwards.
Out[17]:
In [18]:
lis[1:4] # Extract elements from the second element up to
# (but not including the fifth).
Out[18]:
In [19]:
lis
Out[19]:
In [20]:
# Adding an element to the end of a list.
lis.append(6)
lis
Out[20]:
In [21]:
# Adding a list to the end of a list.
lis.extend([7,8,9])
lis
Out[21]:
In [22]:
# Removing an element from the end of a list.
element = lis.pop()
(element, lis)
Out[22]:
In [23]:
# Changing an element in a list.
lis[3] = 42
lis
Out[23]:
In [24]:
tup[0] = 0
In [25]:
tup.append("fourth")
Trying to add or change an element in a tuple results in an error. Tuples cannot be changed after they are constructed, hence they are immutable unlike lists.
In [26]:
range(10)
Out[26]:
In [27]:
range(5, 15)
Out[27]:
In [28]:
range(4, 24, 2)
Out[28]:
In [29]:
x = ["a", "b", "c"]
y = [1 , 2, 3]
zip(x, y)
Out[29]:
Notice how the first elements of x
and y
are "zipped" together into a tuple in the new list, as are the second elements, and the third elements.
In [30]:
zip(x, y, ["Do", "Re", "Mi"])
Out[30]:
In [31]:
x
Out[31]:
In [32]:
list(enumerate(x))
Out[32]:
In [33]:
lis
Out[33]:
In [34]:
lis_copy = lis
lis_copy.append(9)
lis_copy
Out[34]:
As expected lis_copy
now has 9 at the end of it.
In [35]:
lis
Out[35]:
However now lis
also has 9 at the end of it. The line
lis_copy = lis
makes lis_copy
point to the same underlying list as lis
. What's needed here is a copy of the list. There are many ways of copying a list in Python, one of which follows.
In [36]:
lis_copy = lis[:]
lis_copy.pop()
print(lis)
print(lis_copy)
Now the change in lis_copy
does not affect lis
.